package com.anbinh.doctor.fragment.lichhen;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.anbinh.doctor.R;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
import com.wangjie.wheelview.WheelView;

public class FragmentAddTamhuy extends Fragment {
	private static final int START = 1;
	private static FragmentAddTamhuy fragment = null;
	private View mView;
	private WheelView mHourStart;
	private WheelView mMinStart;
	private CircleProgressBar loadding;

	private WheelView mHourStop;
	private WheelView mMinStop;
	private Calendar c;

	private Button btnBatdau;
	private Button btnKethuc;
	private EditText edLydotamhoan;

	private LinearLayout line_start;
	private LinearLayout line_stop;
	private String hour_start;
	private String min_start;

	private String hour_end;
	private String min_end;

	public static FragmentAddTamhuy newInstance() {
		if (fragment == null) {
			fragment = new FragmentAddTamhuy();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		hour_start = "10";
		min_start = "00";

		hour_end = "10";
		min_end = "30";

	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_add_tam_huy, null);
		line_start = (LinearLayout) mView.findViewById(R.id.line_start);
		line_stop = (LinearLayout) mView.findViewById(R.id.line_stop);
		edLydotamhoan = (EditText) mView.findViewById(R.id.edLydotamhoan);
		loadding = (CircleProgressBar) mView.findViewById(R.id.loadding);
		line_start.setVisibility(View.VISIBLE);
		line_stop.setVisibility(View.GONE);

		btnBatdau = (Button) mView.findViewById(R.id.btnBatdau);
		btnKethuc = (Button) mView.findViewById(R.id.btnKetthuc);
		btnBatdau.setSelected(true);
		btnKethuc.setSelected(false);

		mHourStart = (WheelView) mView.findViewById(R.id.hour_start);
		mMinStart = (WheelView) mView.findViewById(R.id.min_start);
		mHourStop = (WheelView) mView.findViewById(R.id.hour_stop);
		mMinStop = (WheelView) mView.findViewById(R.id.min_stop);

		setDataWhell(mHourStart, 24,START);
		setDataWhell(mMinStart, 60,START);
		setDataWhell(mHourStop, 24,-1);
		setDataWhell(mMinStop, 60,-1);

		btnBatdau.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				btnBatdau.setSelected(true);
				btnKethuc.setSelected(false);
				line_start.setVisibility(View.VISIBLE);
				line_stop.setVisibility(View.GONE);
				mHourStart.setSeletion(Integer.parseInt(hour_start));
				mMinStart.setSeletion(Integer.parseInt(min_start));
			}
		});

		btnKethuc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				btnBatdau.setSelected(false);
				btnKethuc.setSelected(true);
				line_start.setVisibility(View.GONE);
				line_stop.setVisibility(View.VISIBLE);
				c = Calendar.getInstance();
				mHourStop.setSeletion(Integer.parseInt(hour_end));
				mMinStop.setSeletion(Integer.parseInt(min_end));
			}
		});

		mHourStart.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				hour_start = item;
			}
		});

		mMinStart.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				min_start = item;
			}
		});

		mHourStop.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				hour_end = item;
			}
		});

		mMinStop.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				min_end = item;
			}
		});
		Button btnTieptuc = (Button) mView.findViewById(R.id.btnTieptuc);
		btnTieptuc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String mLydo = edLydotamhoan.getText().toString().trim();
				if (mLydo.isEmpty()) {
					Utils.showMessage(getActivity(), "Vui lòng nhập lý do");
					return;
				}
				UserLogin userLogin = Utils.getAccountModel(getActivity(),
						Utils.KEY_USER);
				loadding.setVisibility(View.VISIBLE);
				Utils.disableEnableControls(false, mView);

				String date = FragmentLichhen.newInstance().getCurrentDay();
				ServerController.getInstance(getActivity()).requestPostTamHoan(
						userLogin.getData().getToken(),
						FragmentLichhen.newInstance().getCurrentDay(),
						hour_start + ":" + min_start, hour_end + ":" + min_end,
						mLydo, new OnRequest<DataResponse>() {

							@Override
							public void onSuccess(DataResponse data) {
								loadding.setVisibility(View.INVISIBLE);
								Utils.disableEnableControls(true, mView);

								if (data.getResponsecode() >= 0) {
									Intent intent = new Intent();
									intent.putExtra(Utils.KEY,
											Utils.ONRESUME_LICHHEN);
									getActivity().setResult(Integer.MAX_VALUE,
											intent);
									getActivity().finish();
								}
							}

							@Override
							public void onError(int errorCode, String message) {
								loadding.setVisibility(View.INVISIBLE);
								Utils.disableEnableControls(true, mView);

							}
						});
			}
		});
		Utils.goneKeyBoard(getActivity());

		return mView;
	}

	public void setDataWhell(WheelView wheelView, int last, int key) {
		c = Calendar.getInstance();
		wheelView.setOffset(1);
		if (last == 24) {
			if(key==START){
				wheelView.setSeletion(Integer.parseInt(hour_start));
			}else{
				wheelView.setSeletion(Integer.parseInt(min_start));
			}
		} else {
			wheelView.setSeletion(0);
		}
		wheelView.setItems(getData(last));
	}

	public List<String> getData(int last) {
		List<String> mHour = new ArrayList<String>();
		for (int i = 0; i <= last; i++) {
			if (i < 10) {
				mHour.add("0" + i);
			} else {
				mHour.add(i + "");
			}
		}
		return mHour;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

	}

}
